<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//process
$error = false;
$action = (isset($_GET['action']) ? $_GET['action'] : '');
$mainpart = '';
$content_wrap = array();
//request uri
//$request_uri = SYS_RT_REQUEST_URI;
if( strpos(SYS_RT_REQUEST_URI, '?') == false ) {
	$request_uri = SYS_RT_REQUEST_URI;
} else {
	$request_uri = substr(SYS_RT_REQUEST_URI, 0, strpos(SYS_RT_REQUEST_URI, '?'));
}
//filter
$request_uri = ereg_replace("(\/{2,})", "/", $request_uri);
$request_uri = ereg_replace("(^\/)", "", $request_uri);
//friendly
if( CONFIG_URL_DIRECTORY == true ) {

} else {
	$request_uri = str_replace('~', '/', $request_uri);
}
//
//if( util_not_null($request_uri)
//do not use the function util_xxx
if( strlen($request_uri) > 0 && (substr($request_uri, 0, strlen('index.php')) != 'index.php') ) {
	$link_result = util_parse_link($request_uri);
	$request_type = $link_result['type'];
	$request_link = $link_result['link'];
} else {
	$request_type = '';
	$request_link = '';
}
$request_path = $request_type;
$request_param = $_SERVER['QUERY_STRING'];
//$request_redirect = substr($_SERVER['REDIRECT_URL'], strlen(CONFIG_URL_PREFIX));
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
$request_link_class = '';
$request_link_subclass = '';
$request_link_param = array();
$request_link_parse = str_replace(CONFIG_URL_SUFFIX, '', $request_link);
if( strpos($request_link_parse, '?') == false ) {
	$request_link_parse_array = explode('/', $request_link_parse);
} else {
	$request_link_parse_array = explode('/', substr($request_link_parse, 0, strpos($request_link_parse, '?')));
}
$request_link_class = $request_link_parse_array['0'];
$request_link_subclass = $request_link_parse_array['1'];
if( sizeof($request_link_parse_array) > 2 ) {
	$request_link_param = array_slice($request_link_parse_array, 2);
}
if( $request_link_class != '' ) {
	$request_path = $request_type . '/' . $request_link_class . '/';
}
//
$id = trim(strtolower(util_get_param($_REQUEST, 'id')));
$action = trim(strtolower(util_get_param($_REQUEST, 'action')));
$type = trim(strtolower(util_get_param($_REQUEST, 'type')));
$operation = trim(strtolower(util_get_param($_REQUEST, 'op')));
$page = trim(strtolower(util_get_param($_GET, 'page')));
if( !is_numeric($page) ) {
	$page = 1;
}
$length = trim(strtolower(util_get_param($_REQUEST, 'length')));
if( !is_numeric($length) || $length < 10 ) {
	$length = 10;
} else {
	$length = 10;
}
//
$limit_page = $page;
$limit_length = $length;
$limit_start = ($limit_page - 1) * $limit_length;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//INJECTION
if( $request_type == 'admin' ) {
	//
} else {
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/news/news.class.php');
	$mObj = new NewsBase();
	$mObj->format = true;
	$mObj->setQueryLimitLength(5);
	$news_items = $mObj->cache('getItems', array('', 0));
	
	$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'lc_news_items', $news_items);
	
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/product/product.category.class.php');
	$mObj = new ProductCategoryBase();
	$mObj->format = true;
	$mObj->setQueryLimitLength(10);
	$categories = $mObj->cache('getCategories', array('', 0));
	
	$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'lc_categories', $categories);
	$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'tm_ps_categories', $categories);
	
	/*
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/gallery/gallery.category.class.php');
	$mObj = new GalleryCategoryBase();
	$mObj->format = true;
	$mObj->setQueryLimitLength(5);
	$categories = $mObj->cache('getCategories', array('', 0));
	
	$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'tm_gs_categories', $categories);
	
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/gallery/gallery.album.class.php');
	$mObj = new GalleryAlbumBase();
	$mObj->format = true;
	$mObj->setQueryLimitLength(5);
	$items = $mObj->cache('getItems', array('', 0));
	
	$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'tm_gs_albums', $items);
	*/
	
	/*
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.category.class.php');
	$mcObj = new ContentCategoryBase();
	$mcObj->format = true;
	$mcObj->setQueryLimitLength(10);
	$categories = $mcObj->cache('getCategories', array('', 0));
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.class.php');
	$mObj = new ContentBase();
	$mObj->format = true;
	$mObj->setQueryLimitLength(10);
	foreach($categories as $k => $category) {
		$categories[$k]['items'] = $mObj->getItems(array('cid' => $category['categories_id']));
	}
	
	$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'tm_categories', $categories);
	*/
	
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/menu/menu.tree.class.php');
	$mObj = new MenuTreeBase();
	$mObj->format = true;
	$mObj->setQueryLimitLength(10);
	$tm_items = $mObj->cache('getItemsThreeLevel', array(0, '', 0));
	
	$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'tm_items', $tm_items);
	
	/*
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.class.php');
	$mObj = new ContentBase();
	$mObj->format = true;
	$item = $mObj->getItem($request_type, 'link');
	if( $item['id'] > 0 ) {
		$request_link_class = $request_type;
		$request_type = 'a';
	}
	*/
	
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/url/url.class.php');
	$mObj = new URLBase();
	$mObj->format = true;
	$mObj->dispatch();
}

//GLOBAL
$param = array(
	'name' => 'context_wrap', 
	'value' => array(
		'IMAGEURL' => CONFIG_URL_HTTP . CONFIG_DIR_IMAGES, 
		'PRODUCTURL' => CONFIG_URL_HTTP . 'product/', 
		'PRODUCTSURL' => CONFIG_URL_HTTP . 'products/', 
		'PITEMURL' => CONFIG_URL_HTTP . 'product/item/', 
		'CATEGORYURL' => CONFIG_URL_HTTP . 'autoitem/', 
		'CARTURL' => CONFIG_URL_HTTP . 'cart/', 
		'CHECKOUTURL' => CONFIG_URL_HTTP . 'checkout/', 
		'SHOPURL' => CONFIG_URL_HTTP . 'shop/', 
		'MYURL' => CONFIG_URL_HTTP . SYS_RT_LANG_URI . 'my/', 
		'ADMINURL' => CONFIG_URL_HTTP . SYS_RT_LANG_URI . 'admin/', 
		'MEMBERURL' => CONFIG_URL_HTTP . SYS_RT_LANG_URI . 'member/', 
		//
		'PRODUCTIMAGE' => CONFIG_URL_HTTP . CONFIG_DIR_PRODUCTS_IMAGES, 
		'PRODUCTORIGINIMAGE' => 	CONFIG_URL_HTTP . CONFIG_DIR_PRODUCTS_ORIGINIMAGES,
		'PRODUCTTHUMBIMAGE' => 	CONFIG_URL_HTTP . CONFIG_DIR_PRODUCTS_THUMBIMAGES,
		'PRODUCTRESIZEIMAGE' => 	CONFIG_URL_HTTP . CONFIG_DIR_PRODUCTS_RESIZEIMAGES,
		//
		'PRODUCTIMAGEFS' => CONFIG_PATH . CONFIG_DIR_PRODUCTS_IMAGES, 
		'PRODUCTORIGINIMAGEFS' => 	CONFIG_PATH . CONFIG_DIR_PRODUCTS_ORIGINIMAGES,
		'PRODUCTTHUMBIMAGEFS' => 	CONFIG_PATH . CONFIG_DIR_PRODUCTS_THUMBIMAGES,
		'PRODUCTRESIZEIMAGEFS' => 	CONFIG_PATH . CONFIG_DIR_PRODUCTS_RESIZEIMAGES,
		//
		'PRODUCTCATEGORYIMAGE' => 	CONFIG_URL_HTTP . CONFIG_DIR_PRODUCTS_CATEGORIES_IMAGES,
		//
		'PRODUCTBRANDIMAGE' => CONFIG_URL_HTTP . CONFIG_DIR_BRANDS_IMAGES,
		'PRODUCTBRANDIMAGEFS' => CONFIG_PATH . CONFIG_DIR_BRANDS_IMAGES,
		//
		'RESOURCEIMAGE' => CONFIG_URL_HTTP . CONFIG_DIR_RESOURCES, 
		'ALBUMIMAGE' => 		CONFIG_URL_HTTP . CONFIG_DIR_ALBUMS_IMAGES,
		'ALBUMORIGINIMAGE' => 	CONFIG_URL_HTTP . CONFIG_DIR_ALBUMS_ORIGINIMAGES,
		'ALBUMTHUMBIMAGE' => 	CONFIG_URL_HTTP . CONFIG_DIR_ALBUMS_THUMBIMAGES,
		'ALBUMRESIZEIMAGE' => 	CONFIG_URL_HTTP . CONFIG_DIR_ALBUMS_RESIZEIMAGES,
		
		//
		'PLAYER' => array(
				'URL' => CONFIG_URL_HTTP . 'player/v1/player.swf',
					),
		//
		'STYLEURL' => CONFIG_URL_HTTP . CONFIG_DIR_STYLES . CONFIG_STYLE . '/',
		'STYLEIMG' => CONFIG_URL_HTTP . CONFIG_DIR_STYLES . CONFIG_STYLE . '/img/',
		'STYLEIMAGE' => CONFIG_URL_HTTP . CONFIG_DIR_STYLES . CONFIG_STYLE . '/images/',
		'STYLEICON' => CONFIG_URL_HTTP . CONFIG_DIR_STYLES . CONFIG_STYLE . '/images/icons/',
			));
$injectObj->add('func_get_tpl_pagelayout_content', $param);

//
$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'category_image_url', CONFIG_URL_HTTP . CONFIG_DIR_PRODUCTS_CATEGORIES_IMAGES);
$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'product_category_image_url', CONFIG_URL_HTTP . CONFIG_DIR_PRODUCTS_CATEGORIES_IMAGES);

//
if( is_array($_GET) ) $_GET = func_db_prepare_input($_GET);
if( is_array($_POST) ) $_POST = func_db_prepare_input($_POST);
if( is_array($_REQUEST) ) $_REQUEST = func_db_prepare_input($_REQUEST);
if( is_array($_COOKIE) ) $_COOKIE = func_db_prepare_input($_COOKIE);

//
$rt_current_userid = $_SESSION['suser']->userid;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
$tpl->register_block("checklogin", "func_tpl_checklogin");
$tpl->register_block("haslogin", "func_tpl_haslogin");
$tpl->register_block("notlogin", "func_tpl_notlogin");
//$tpl->register_outputfilter("func_tpl_outputfilter");

//
if( $SETTINGS['bs_closed'] == '1' && ($request_type != 'admin' && $request_type != 'ws.php') ) {
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'frontend.main.class.php');
	$feObj = new FrontendMainBase();
	$feObj->close();
	exit();
}

//crons
//include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.cron.class.php');
//$cronObj = new CoreCronBase();
//$cronObj->run();
//
?>